/*
 * HashTimer.h
 *
 *  Created on: Jul 8, 2024
 *      Author: root
 */

#ifndef BSP_TIMER_HASHTIMER_H_
#define BSP_TIMER_HASHTIMER_H_
#include "basic_header.h"

namespace bsp {

typedef std::function<void()> fun_callback;

class HashTimer {
public:
	struct TimerNode {
		int id;
		fun_callback cb;
		uint64_t timeout;
	};
private:
	std::vector<struct TimerNode> vec;
	std::unordered_map<int, size_t> hash;

	uint32_t max_size;
	uint32_t cur_size;

	void reorder(int i);
	int findPos(int start, int end, int cur);
	void print();
public:
	HashTimer(uint32_t max_size);
	virtual ~HashTimer();

	void add(int id, fun_callback &&cb, uint32_t timeout);
	void mod(int id, fun_callback &&cb, uint32_t timeout);
	void del(int id);

	void tick();
	int getNextTick();

	static void test1();
};

} /* namespace bsp */

#endif /* BSP_TIMER_HASHTIMER_H_ */
